package com.u2b.servlet.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;




public class SendToClient
{
    /*
     * 静态方法是唯一的,所以多线程会出现互斥争用
     */
    public void sendMessage(HttpServletRequest request, HttpServletResponse resp, Object o)
    {
        String location = "/result/return.jsp";
        if (null == o)
        {
            request.setAttribute("return", 0); // 0表示服务器出现错误
        }
        else
        {
            String json = JsonUtils.createJsonByObject("result", o);
            request.setAttribute("return", json);
        }
        try
        {
            request.getRequestDispatcher(location).forward(request, resp);
        }
        catch (Exception e)
        {
            throw new RuntimeException("发送消息到客户端出现异常", e);
        }
    }
}
